package mo.leetcode_hot;

import java.util.Stack;

public class L394 {

    public String decodeString(String s) {
        Stack<Integer> nums = new Stack<>();
        Stack<StringBuilder> str = new Stack<>();
        int multi = 0;
        StringBuilder ans = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (Character.isDigit(c)) {
                multi = multi * 10 + (c - '0');
            } else if (c == '[') {
                str.add(ans);
                nums.push(multi);
                multi = 0;
                ans = new StringBuilder();
            } else if (Character.isAlphabetic(c)) {
                ans.append(c);
            } else {
                StringBuilder ansTmp = str.pop();
                Integer count = nums.pop();
                for (int i = 0; i < count; i++) {
                    ansTmp.append(ans);
                }
                ans = ansTmp;
            }
        }
        return ans.toString();
    }

    public static void main(String[] args) {
        L394 test = new L394();
        System.out.println(test.decodeString("abc3[cd]xyz"));
    }

}
